var express = require('express');
var { goods,address,users,cart } = require('../model/zhaojunjie')

var router = express.Router();


router.get('/pros', async (req, res) => {
  let data = await goods.find({}).toArray()
  res.send({
    code: 200,
    data
  })
})
router.get('/cate_pro', async (req, res) => {
  let data = await goods.find({}).toArray()
  data=data.filter(ele => {
    return ele.activity==req.query.cate
  })
  console.log(data);
  res.send({
    code: 200,
    data
  })
})
router.get('/address', async (req, res) => {
  let data = await users.find({}).toArray()
  data = data.filter(ele => {
    return ele._id == req.query.userId
  })[0]
  console.log(data);
  res.send({
    code: 200,
    data
  })
})
router.post('/cart', async (req, res) => {
  console.log(req.body);
  let data = await cart.find({}).toArray()
  data = data.filter(ele => {
    return (ele.name == req.body.name)&&(ele.uid==req.body.uid)
  })
  if (data.length == 0) {
    await cart.insertOne(req.body)
  } else {
    await cart.updateOne({uid:data[0].uid},{$set:{buynum:data[0].buynum+1}})
  }
  res.send({
    code:200
  })

  })
/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;
